package com.dongdongshop.rocketmqConsumer;

import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
import com.yunpian.sdk.model.SmsSingleSend;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@RocketMQMessageListener(topic = "message-topic",consumerGroup = "get-message")
public class RocketMQConsumer implements RocketMQListener<String> {
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(String phone) {
        String key = phone;

        //初始化clnt,使用单例方式
        YunpianClient clnt = new YunpianClient("8de830354705612fdb9ba4728de8384a").init();
        String code = RandomStringUtils.randomNumeric(6);
        redisTemplate.opsForValue().set(key,code);
        redisTemplate.expire(key,10, TimeUnit.MINUTES);
        //发送短信API
        Map<String, String> param = clnt.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        param.put(YunpianClient.TEXT, "【尉晓祥】您的验证码是"+code+"。如非本人操作，请忽略本短信");
        Result<SmsSingleSend> r = clnt.sms().single_send(param);


        //获取返回结果，返回码:r.getCode(),返回码描述:r.getMsg(),API结果:r.getData(),其他说明:r.getDetail(),调用异常:r.getThrowable()

        //账户:clnt.user().* 签名:clnt.sign().* 模版:clnt.tpl().* 短信:clnt.sms().* 语音:clnt.voice().* 流量:clnt.flow().* 隐私通话:clnt.call().*

        //释放clnt
        clnt.close();
    }
}
